<template>
  <div id="visitor">
    <Loading v-if="total == null"></Loading>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">评论管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>评论列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索评论区域 -->
      <el-row>
        <el-col :span="8"
          ><el-input
            placeholder="请输入评论关键字查询"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getCommentList"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="getCommentList"
              ></el-button>
            </template> </el-input
        ></el-col>
      </el-row>

      <!-- 评论列表 -->
      <el-table :data="commentList" border stripe class="mt10">
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column label="评论内容" prop="content"></el-table-column>
        <el-table-column
          label="评论文章"
          prop="object.title"
          width="250px"
          align="center"
        ></el-table-column>
        <el-table-column label="评论时间" width="190px" align="center">
          <template #default="scope">
            {{ formatTime(scope.row.createdAt) }}
          </template></el-table-column
        >
        <el-table-column label="操作" width="140px" align="center">
          <template #default="scope">
            <el-button
              type="info"
              icon="el-icon-info"
              size="mini"
              @click="getMore(scope.row.visitor_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteComment(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 20, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 访客详情对话框 -->
    <el-dialog v-model="isShowVisitorDetailDialog" width="600px">
      <el-descriptions title="访客信息" direction="vertical" :column="2" border>
        <el-descriptions-item label="访客昵称:">{{
          visitorInfo.visitorName
        }}</el-descriptions-item>
        <el-descriptions-item label="头像"
          ><div class="img-wrapper">
            <img :src="visitorInfo.avatarUrl" /></div
        ></el-descriptions-item>
        <el-descriptions-item label="注册时间:">{{
          formatTime(visitorInfo.createdAt)
        }}</el-descriptions-item>
        <el-descriptions-item label="最后活跃:">{{
          formatTime(visitorInfo.lastActive)
        }}</el-descriptions-item>
        <el-descriptions-item label="邮箱:">{{
          visitorInfo.email
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from "@vue/reactivity";
import { formatTime } from "../../libs/utils";
import { getData, deleteData } from "../../services/http";
import { getCurrentInstance, onBeforeMount } from "@vue/runtime-core";
export default {
  name: "CommentList",
  setup() {
    const { proxy } = getCurrentInstance();
    const state = reactive({
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: null,
      commentList: [],
      visitorInfo: {},
    });

    const isShowVisitorDetailDialog = ref(false);
    const stateAsRefs = toRefs(state);
    const getCommentList = async () => {
      const { data: res } = await getData("rest/comment", {
        params: state.queryInfo,
      });
      state.commentList = res.data;
      state.total = res.total;
    };
    const getVisitorDetail = async (id) => {
      const { data: res } = await getData(`rest/visitor/${id}`);
      state.visitorInfo = res;
    };
    const getMore = (id) => {
      isShowVisitorDetailDialog.value = true;
      getVisitorDetail(id);
    };
    const deleteComment = async (row) => {
      const { content, _id } = row;
      proxy
        .$confirm(`此操作将永久删除评论 ${content}, 是否继续?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(async () => {
          await deleteData(`rest/comment/${_id}`, `已成功删除评论 ${content}`);
          getCommentList();
        })
        .catch(() => {
          proxy.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    };
    const handleCurrentChange = (newPage) => {
      state.queryInfo.pageNum = newPage;
      getCommentList();
    };
    const handleSizeChange = (newSize) => {
      state.queryInfo.pageSize = newSize;
      getCommentList();
    };
    onBeforeMount(() => {
      getCommentList();
    });
    return {
      ...stateAsRefs,
      isShowVisitorDetailDialog,
      handleCurrentChange,
      handleSizeChange,
      formatTime,
      getMore,
      deleteComment,
      getCommentList,
    };
  },
};
</script>

<style>
.img-wrapper img {
  width: 32px;
  height: 32px;
  object-fit: cover;
}
</style>